﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC.DuctTerminalTag
{
	public partial class FrmDuctTerminalTag : System.Windows.Forms.Form
	{
		public FrmDuctTerminalTag()
		{
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.InitializeComponent();
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{3B9EAA2B-D5A9-4BCC-8896-D62FE23DC9FB}", null);
			this.setting = new UserSetting();
			base.Icon = Resource.YArchitechIcon;
		}

		private void btnMarkSet_Click(object sender, EventArgs e)
		{
			FormTotalMarkSetting formTotalMarkSetting = new FormTotalMarkSetting(ShowMarkType.Terminal);
			formTotalMarkSetting.ShowDialog();
			if (formTotalMarkSetting.DialogResult == DialogResult.OK)
			{
				this.setting = new UserSetting();
				this.markSetting = this.setting.GetTerminalMarkSetting();
				this.ContorlPrevieImage();
			}
			if (this.contorProcess)
			{
				this.ClickESC();
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void chkName_CheckedChanged(object sender, EventArgs e)
		{
			this.ContorlPrevieImage();
			if (this.contorProcess)
			{
				this.ClickESC();
			}
		}

		private void FrmDuctTerminalTag_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.WriteData();
			if (this.contorProcess)
			{
				this.ClickESC();
			}
			this.contorProcess = true;
		}

		private void LoadPreviewIamge()
		{
			this.dicPreviewIamge.Add("SizeQuantity", YArchitech.HVAC.Properties.Resources.SizeQuantity);
			this.dicPreviewIamge.Add("NameSizeQuantity", YArchitech.HVAC.Properties.Resources.NameSizeQuantity);
			this.dicPreviewIamge.Add("SizeQuantityAirAmount", YArchitech.HVAC.Properties.Resources.SizeQuantityAirAmount);
			this.dicPreviewIamge.Add("NameSizeQuantityAirAmount", YArchitech.HVAC.Properties.Resources.NameSizeQuantityAirAmount);
			this.dicPreviewIamge.Add("DrawLineBoxSizeQuantity", YArchitech.HVAC.Properties.Resources.DrawLineBoxSizeQuantity);
			this.dicPreviewIamge.Add("DrawLineBoxNameSizeQuantity", YArchitech.HVAC.Properties.Resources.DrawLineBoxNameSizeQuantity);
			this.dicPreviewIamge.Add("DrawLineBoxSizeQuantityAirAmount", YArchitech.HVAC.Properties.Resources.DrawLineBoxSizeQuantityAirAmount);
			this.dicPreviewIamge.Add("DrawLineBoxNameSizeQuantityAirAmount", YArchitech.HVAC.Properties.Resources.DrawLineBoxNameSizeQuantityAirAmount);
		}

		private void ContorlPrevieImage()
		{
			string strKey = "";
			if (this.markSetting.DrawLineBox == "true")
			{
				strKey += "DrawLineBox";
			}
			if (this.chkName.Checked)
			{
				strKey += "Name";
			}
			strKey += "SizeQuantity";
			if (this.chkAirAmount.Checked)
			{
				strKey += "AirAmount";
			}
			if (this.dicPreviewIamge.FirstOrDefault((KeyValuePair<string, Image> p) => p.Key == strKey).Value != null)
			{
				this.picPreview.Image = this.dicPreviewIamge[strKey];
				return;
			}
			this.picPreview.Image = null;
		}

		private void ClickESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			FrmDuctTerminalTag.SendMessage(DuctTerminalTagCommand.wndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			FrmDuctTerminalTag.keybd_event((byte)bVk, 0, dwFlags, 0);
			FrmDuctTerminalTag.keybd_event((byte)bVk, 0, dwFlags2, 0);
			this.contorProcess = false;
		}

		private void Initialize()
		{
			this.markSetting = this.setting.GetTerminalMarkSetting();
			this.commFont = this.setting.GetFontSetting();
			this.LoadPreviewIamge();
			this.ReadData();
			this.ContorlPrevieImage();
		}

		private void WriteData()
		{
			string[] data = new string[]
			{
				this.chkName.Checked ? "1" : "0",
				this.chkAirAmount.Checked ? "1" : "0"
			};
			this.setting.WriteDuctTerminalTagData(data);
		}

		private void ReadData()
		{
			string[] array = this.setting.ReadDuctTerminalTagData();
			this.chkName.Checked = (array[0] == "1");
			this.chkAirAmount.Checked = (array[1] == "1");
		}

		private void FrmDuctTerminalTag_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			this.Initialize();
			base.KeyPreview = true;
		}

		private void FrmDuctTerminalTag_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				this.WriteData();
				if (this.contorProcess)
				{
					this.ClickESC();
				}
				this.contorProcess = true;
			}
		}

		private void FrmDuctTerminalTag_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		private UserSetting setting;

		public TerminalMarkSet markSetting;

		private Dictionary<string, Image> dicPreviewIamge = new Dictionary<string, Image>();

		public CommonFont commFont;

		public bool contorProcess;

		private ControlInfoManager m_thisManger;
	}
}
